package org.gcb.core.ui.dnd;

import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.io.IOException;
import org.gcb.core.model.Item;
import org.gcb.core.model.Toon;
import org.openide.explorer.view.IconView;
import org.openide.util.Exceptions;

/**
 *
 * @author Jason
 */

public class BackpackDropTargetListener extends DropTargetAdapter {
    private DropTarget dropTarget;
    private IconView iconView;
    private Toon toon;

    public BackpackDropTargetListener(IconView iconView, Toon toon) {
	this.iconView = iconView;
	this.toon = toon;

	dropTarget = new DropTarget(iconView, DnDConstants.ACTION_MOVE, this, true);
    }

    @Override
    public void drop(DropTargetDropEvent dtde) {
	Transferable tr = dtde.getTransferable();
	try {
	    Item item = Item.class.cast(tr.getTransferData(Item.ITEM_DATA_FLAVOR));
	    if (dtde.isDataFlavorSupported(Item.ITEM_DATA_FLAVOR)) {
		dtde.acceptDrop(DnDConstants.ACTION_MOVE);
		this.toon.getEquipment().getCarried().add(item);
		return;
	    }
	    dtde.rejectDrop();
	} catch (UnsupportedFlavorException ex) {
	    Exceptions.printStackTrace(ex);
	} catch (IOException ex) {
	    Exceptions.printStackTrace(ex);
	}
	dtde.rejectDrop();
    }

}